/**
 * 
 */
package uk.ac.manchester.tewpl.iceiface.server.service;

import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dozer.DozerBeanMapper;
import org.explorium.api.model.KBCommunity;
import org.explorium.api.model.KBInfo;
import org.explorium.api.model.KBWorkspace;
import org.explorium.core.model.KBCommunityImpl;
import org.explorium.core.model.KBInfoImpl;
import org.explorium.core.model.KBWorkspaceImpl;
import org.explorium.core.repository.db.KBCommunityDBRepository;
import org.explorium.core.repository.db.KBInfoDBRepository;
import org.springframework.beans.factory.annotation.Autowired;

import uk.ac.manchester.tewpl.iceiface.client.model.KBCommunityModel;
import uk.ac.manchester.tewpl.iceiface.client.model.KBInfoModel;
import uk.ac.manchester.tewpl.iceiface.client.model.KBWorkspaceModel;
import uk.ac.manchester.tewpl.iceiface.client.service.CommunityService;
import uk.ac.manchester.tewpl.iceiface.server.util.DozerListMapper;

/**
 * @author roland
 * 
 */
public class CommunityServiceImpl implements CommunityService {
	private static final Log LOG = LogFactory
			.getLog(CommunityServiceImpl.class);
	@Autowired
	private KBCommunityDBRepository communityRepository;
	@Autowired
	private KBInfoDBRepository kbInfoRepository;
	@Autowired
	DozerBeanMapper mapper;

	@Autowired
	DozerListMapper listMapper;

	public void setCommunityRepository(
			KBCommunityDBRepository communityRepository) {
		this.communityRepository = communityRepository;
	}

	public void setMapper(DozerBeanMapper mapper) {
		this.mapper = mapper;
	}

	public void setListMapper(DozerListMapper listMapper) {
		this.listMapper = listMapper;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.explorium.web.client.service.CommunityService#deleteCommunity(org
	 * .explorium.model.KBCommunity)
	 */
	public void deleteCommunity(KBCommunityModel community) {
		communityRepository.deleteById(community.getId());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.explorium.web.client.service.CommunityService#findAllCommunities()
	 */
	public Collection<KBCommunityModel> findAllCommunities() {
		Collection<KBCommunity> list = communityRepository.findAll();
		return listMapper.map(list, KBCommunityModel.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.explorium.web.client.service.CommunityService#findCommunityById(java
	 * .lang.Long)
	 */
	public KBCommunityModel findCommunityById(Long id) {
		return (KBCommunityModel) mapper.map(communityRepository.findById(id),
				KBCommunityModel.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.explorium.web.client.service.CommunityService#findCommunityByName
	 * (java.lang.String)
	 */
	public KBCommunityModel findCommunityByName(String name) {
		return (KBCommunityModel) mapper.map(communityRepository
				.findByName(name), KBCommunityModel.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.explorium.web.client.service.CommunityService#saveCommunity(org.explorium
	 * .model.KBCommunity)
	 */
	public KBCommunityModel saveCommunity(KBCommunityModel community) {
		KBCommunity c = new KBCommunityImpl();
		mapper.map(community, c);
		c = communityRepository.update(c);
		return (KBCommunityModel) mapper.map(c, KBCommunityModel.class);
	}
	
	public KBWorkspaceModel createWorkspace(String uri, KBCommunityModel community, KBInfoModel kbInfo){
		KBCommunity c = communityRepository.findById(community.getId());
		KBInfo kb = kbInfoRepository.findById(kbInfo.getId());
		
		if (uri == null || uri.equals("")) {
			uri = "workspace:" + c.getName().replaceAll("\\W", "") + ":" + kb.getName().replaceAll("\\W", "");
		}
		
		KBWorkspace ws = c.addKnowledgeBase(uri,kb);
		communityRepository.update(c);
		
		KBWorkspaceModel wsm = (KBWorkspaceModel) mapper.map(ws, KBWorkspaceModel.class);
		return wsm;
	}

	/**
	 * @param kbInfoRepository the kbInfoRepository to set
	 */
	public void setKbInfoRepository(KBInfoDBRepository kbInfoRepository) {
		this.kbInfoRepository = kbInfoRepository;
	}

}
